<template>
    <div class="box">
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple" style="height: 77px">
                    <span style="line-height: 77px; font-size: 14px"> </span>
                    <span style="margin-left: 20px">
                        <el-button type="primary" size="small" @click="addCarUser">用车申请</el-button>
                    </span>
                </div>
            </el-col>
        </el-row>
        <!-- 搜索栏start -->
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple">
                    <div style="width: 100%; margin-left: 20px;">
                        <span style="margin-left: 20px;">
                            <el-form :inline="true" :model="formInline" size="mini" class="demo-form-inline">
                                <!-- &nbsp;&nbsp;&nbsp;&nbsp; -->
                                <el-form-item label="用车人">
                                    <el-input v-model="formInline.nickName" placeholder="请输入车辆名称"></el-input>
                                </el-form-item>
                                <el-form-item label="车牌号">
                                    <el-select v-model="formInline.carNumber" placeholder="车牌号">
                                        <span v-for="(item, index) in carNumbers" :key="index">
                                            <el-option :label="item.carNumber" :value="item.carNumber"></el-option>
                                        </span>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="审批状态">
                                    <el-select v-model="formInline.carState" placeholder="车辆状态">
                                        <span v-for="(item, index) in carStates" :key="index">
                                            <el-option :label="item.label" :value="item.value"></el-option>
                                        </span>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="用车时间">
                                    <el-date-picker v-model="timeArr" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
                                        start-placeholder="开始日期" end-placeholder="结束日期">
                                    </el-date-picker>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                                    <el-button type="primary" @click="reset">重置</el-button>
                                </el-form-item>
                            </el-form>
                        </span>
                    </div>
                </div>
            </el-col>
        </el-row>
        <!-- 搜索栏end -->
        <!-- 表格 -->
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple">
                    <span style="padding-left: 10px">
                        <el-table :data="tableData" border="" style="width:100%;">
                            <el-table-column fixed prop="nickName" label="用车人" width="80">
                            </el-table-column>
                            <el-table-column prop="carNumber" label="车牌号" width="100">
                            </el-table-column>
                            <el-table-column prop="carCause" label="用车事由" width="120">
                            </el-table-column>
                            <el-table-column prop="carStartTime" label="开始时间" width="160">
                            </el-table-column>
                            <el-table-column prop="carEndTime" label="结束时间" width="170">
                            </el-table-column>
                            <el-table-column prop="drivingTime" label="用车时长" width="100">
                            </el-table-column>
                            <el-table-column prop="carState" label="状态" width="140">
                                <template slot-scope="scope">
                                    <span v-if="scope.row.carState == '0'"><el-tag effect="dark">审批中</el-tag></span>
                                    <span v-if="scope.row.carState == '1'"><el-tag effect="dark"
                                            type="success">已通过</el-tag></span>
                                    <span v-if="scope.row.carState == '2'"><el-tag effect="dark"
                                            type="danger">审批拒绝</el-tag></span>
                                    <span v-if="scope.row.carState == '3'"><el-tag effect="dark"
                                            type="warning">审批撤销</el-tag></span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="createTime" label="发起时间" width="170">
                            </el-table-column>
                            <el-table-column fixed="right" label="操作" width="140">
                                <template slot-scope="scope">
                                    <el-button @click="detailsClick(scope.row.id, true)" type="text"
                                        size="small">详情</el-button>
                                    <span v-if="scope.row.carState==0">
                                    &nbsp;
                                    <el-button @click="rejectClick(scope.row.id)" type="text"
                                        size="small">拒绝</el-button>
                                    <el-button @click="consentClick(scope.row.id)" type="text"
                                        size="small">同意</el-button>
                                    </span>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="grid-content bg-purple-dark">
                            <div class="block">
                                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                    :current-page="pageNum" :page-sizes="[5, 10, 20, 50]" :page-size="pageSize"
                                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                                </el-pagination>
                            </div>
                        </div>
                    </span>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            //搜索
            formInline: {},
            //拒绝 同意
            from:{},
            pageNum: 1,
            pageSize: 5,
            total: 1,
            tableData: [],
            carNumbers: [],
            carStates: [{ value: "0", label: "审批中" },
            { value: "1", label: "已通过" },
            { value: "2", label: "审核拒绝" },
            { value: "3", label: "审核撤销" }],
            timeArr:[],
        }
    },
    created() {
        this.getCarUse()
        this.getCarNumber()
    },
    methods: {
        //用车申请
        addCarUser(){
            this.$router.push("generalvehiclemanagement/generalvehicleoperation")
        },
        //获取用车信息
        getCarUse(){
            this.formInline.carStartTime = this.timeArr[0];
            this.formInline.carEndTime = this.timeArr[1];
            axios.post("/caruse/getCarUse?pageSize="+this.pageSize+"&pageNum="+this.pageNum,this.formInline).then((res)=>{
                console.log(res)
                if(res.data.code === 200){
                    this.tableData = res.data.data.records
                    this.total = res.data.data.total
                }else{
                    this.$message.error("查询失败")
                }
            })
        },
        //搜索
        onSubmit() {
            this.getCarUse()
            this.$message.success("查询完成")
        },
        //重置
        reset() {
            this.formInline = {}
            this.timeArr=[]
            this.getCarUse()
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.pageSize = val;
            this.getCarUse();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.pageNum = val;
            this.getCarUse();
        },
        //查询车牌号
        getCarNumber() {
            axios.get("/car/getCarNumber").then((res) => {
                console.log(res)
                this.carNumbers = res.data.data
            })
        },
        //拒绝
        rejectClick(id){
            this.from={}
            this.from.carState = 2
            this.from.id=id
            axios.post("/caruse/updateCarUseState",this.from).then((res)=>{
                console.log(res)
                if(res.data.code === 200){
                    this.$message.success(this.res.data.data)
                }
            })
            this.$router.go(0)
        },
        //同意
        consentClick(id){
            this.from={}
            this.from.carState = 1
            this.from.id=id
            axios.post("/caruse/updateCarUseState",this.from).then((res)=>{
                console.log(res)
                if(res.data.code === 200){
                    this.$message.success(res.data.data)
                }
            })
            this.$router.go(0)
        },
        //详情页
        detailsClick(id){
            this.$router.push({path:"/generalvehiclemanagement/CarParticulars",query:{id:id}})
        }
    }
}
</script>

<style scoped>
/* element-ui样式 */
.el-row {
    margin-bottom: 10px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple {
    background: #fff;
    border-radius: 10px;
}

/* 自定义样式 */
/* * {
    margin: 0px;
    padding: 0px;
  } */
.box {
    width: 98%;
    height: 100%;
    margin: auto;
    /* background-color: red; */
}
</style>